﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WxPayAPI;

namespace WxPayJSAPI.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        #region H5支付

        public ActionResult ResultNotify()
        {
            var ss =  new AliPayController().GetRequestGet();

            return View(ss) ;
        }

        public string payH5()
        {
            Log.Error(this.GetType().ToString(), "==================开始微信H5支付: 统一下单 ===========");


            //若传递了相关参数，则调统一下单接口，获得后续相关接口的入口参数
            H5ApiPay h5pay = new H5ApiPay();
            h5pay.total_fee = 1;//int.Parse((model.Money * 100).ToString("0"));
            var ssUrl = Request.UrlReferrer.OriginalString.ToString();
            h5pay.scene_info = "{ \"h5_info\": { \"type\":\"Wap\",\"wap_url\": \"" + ssUrl + "\",\"wap_name\":\"微信H5支付\"} }";


            string wxJsApiParam = "";
            try
            {
                ////// ////// //////
                var OrderId = "本地订单ID";
                OrderId = new Random().Next(10000, 99999).ToString();
                ////// ////// ////// ////// //////

                WxPayData unifiedOrderResult = h5pay.GetUnifiedOrderResult(OrderId);
                return unifiedOrderResult.ToJson();
            }
            catch (Exception ex)
            {
                Log.Error(this.GetType().ToString(), "==================开始微信支付: 统一下单失败: " + ex.Message + " ===========");
                return "";
            }

            return "";
        }

        #endregion

        public string pay()
        {

            Log.Error(this.GetType().ToString(), "==================开始微信支付: 统一下单 ===========");

            if (System.Web.HttpContext.Current.Session["Openid"] == null || System.Web.HttpContext.Current.Session["Openid"] == "")
            {
                Log.Error(this.GetType().ToString(), "==================开始微信支付: openId失效 ===========");
                //return "微信支付失败,请重新进入微信号支付";
                return "";
            }

            //若传递了相关参数，则调统一下单接口，获得后续相关接口的入口参数
            JsApiPay jsApiPay = new JsApiPay();
            jsApiPay.openid = System.Web.HttpContext.Current.Session["Openid"].ToString();
            Log.Error(this.GetType().ToString(), "openid:" + jsApiPay.openid);
            jsApiPay.total_fee = 1;//int.Parse((model.Money * 100).ToString("0"));

            Log.Error(this.GetType().ToString(), "==================微信支付: openid: " + jsApiPay.openid + "   金额:  " + jsApiPay.total_fee + " ===========");
            string wxJsApiParam = "";
            try
            {
                ////// ////// //////
                var OrderId = "本地订单ID";
                OrderId = new Random().Next(10000, 99999).ToString();
                ////// ////// ////// ////// //////

                WxPayData unifiedOrderResult = jsApiPay.GetUnifiedOrderResult(OrderId);
                wxJsApiParam = jsApiPay.GetJsApiParameters();//获取H5调起JS API参数
                Log.Error(this.GetType().ToString(), "==================开始微信支付: 统一下单成功 " + wxJsApiParam + " ===========");
            }
            catch (Exception ex)
            {
                Log.Error(this.GetType().ToString(), "==================开始微信支付: 统一下单失败: " + ex.Message + " ===========");
                return "";
            }

            return wxJsApiParam.ToString();
        }


        public ActionResult Wx()
        {
            //默认微信公众号打开获取openID的连接---
            //从以下地址打开后才可进行微信支付
            string url = WxHelper.Wx.WxHelper.WXUrlEncode("http://www.lvrongkj.com/Home/RedirectURL");
            WxPayAPI.Log.Error(this.GetType().ToString(), "==================ResultNotify-----url:" + url + "===========");
            //--https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxc32aa525ff254026&redirect_uri=%77%77%77%2e%78%69%61%6f%6d%69%73%68%71%2e%63%6f%6d%2f%48%6f%6d%65%2f%52%65%64%69%72%65%63%74%55%52%4c&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect
            //微信退款申请语法
            //string result = WxHelper.Wx.Refund.Run("4003992001201704288598174038", "", "1", "1");
            return Redirect(url);
        }

        /// <summary>
        /// 跳转页面(微信用)
        /// </summary>
        /// <returns></returns>
        public ActionResult RedirectURL(string code)
        {
            WxPayAPI.Log.Info(this.GetType().ToString(), "==================ResultNotify-----codeId:" + code + "===========");
            string Openid = "";
            if (code != null && code != "")
            {
                WxHelper.Wx.Access_token token = WxHelper.Wx.WxHelper.GetOpenID(code);
                Openid = token.OpenId;
            }
            System.Web.HttpContext.Current.Session["Openid"] = Openid;
            WxPayAPI.Log.Error(this.GetType().ToString(), "==================ResultNotify-----codeOpenId:" + Openid + "===========");
            if (string.IsNullOrWhiteSpace(Openid))
            {
                return null;
            }
            return RedirectToAction("Index", "Home");
        }
    }
}